#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void *worker_thread(void *arg)
{
    printf("This is worker_thread #%ld started\n", (long)arg);
    int sleepTime = rand() % 10 + 1; // 生成1-10的随机数
    sleep(sleepTime); // 线程休眠
	printf("Worker_thread #%ld sleep %d seconds \n", (long)arg, sleepTime);
	pthread_exit(NULL);
}

int main()
{
	const int COUNT = 5;
    pthread_t my_thread[COUNT];
    long id;
    for(id = 1; id <= COUNT; id++) {
        int ret =  pthread_create(&my_thread[id], NULL, &worker_thread, (void*)id);
        if(ret != 0) {
            printf("Error: pthread_create() failed\n");
            exit(EXIT_FAILURE);
        }
    }
    for(id = 1; id <= COUNT; id++) {
        pthread_join(my_thread[id], NULL);
	}
    pthread_exit(NULL);
}
